package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.entity.mysql.ComplianceCheck;
import cn.fintecher.pangolin.service.business.model.request.ComplianceCheckQueryRequest;
import cn.fintecher.pangolin.service.business.model.request.ComplianceCheckRequest;
import cn.fintecher.pangolin.service.business.model.response.ComplianceCheckResponse;
import cn.fintecher.pangolin.service.business.repository.jparepository.ComplianceCheckRepository;
import cn.fintecher.pangolin.service.business.service.BaseService;
import cn.fintecher.pangolin.service.business.service.ComplianceCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Author wangxiangdong
 * @Description 合规检查
 * @Date 2019/10/19 9:46
 **/
@RestController
@RequestMapping("/api/complianceCheckController")
@Api(value = "合规检查", description = "合规检查")
public class ComplianceCheckController {

    @Autowired
    private BaseService baseService;

    @Autowired
    private ComplianceCheckService complianceCheckService;

    @Autowired
    private ComplianceCheckRepository complianceCheckRepository;

    @PostMapping("/createComplianceCheck")
    @ApiOperation(value = "新增配置", notes = "新增配置")
    public ResponseEntity createComplianceCheck(@RequestBody ComplianceCheckRequest complianceCheckRequest, @RequestHeader(value = "X-UserToken") String token) {
        UserModel userModel = baseService.getUserByToken(token);
        complianceCheckService.createComplianceCheck(complianceCheckRequest, userModel);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    @DeleteMapping("/deleteComplianceCheck/{id}")
    @ApiOperation(value = "删除配置", notes = "删除配置")
    public ResponseEntity deleteComplianceCheck(@PathVariable String id, @RequestHeader(value = "X-UserToken") String token) {
        complianceCheckService.deleteComplianceCheck(id);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    @ApiOperation(value = "配置分页查询", notes = "配置分页查询")
    @GetMapping("/getAllComplianceCheck")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<ComplianceCheck>> getAllComplianceCheck(@ApiIgnore Pageable pageable, ComplianceCheckQueryRequest complianceCheckQueryRequest) {
        Sort sort = new Sort(Sort.Direction.DESC, "operateTime");
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        return ResponseEntity.ok().body(complianceCheckRepository.findAll(complianceCheckQueryRequest.generateBooleanBuilder(), pageable));
    }

    @PutMapping("/changeState/{id}")
    @ApiOperation(value = "更改状态", notes = "更改状态")
    public ResponseEntity changeState(@PathVariable String id, @RequestHeader(value = "X-UserToken") String token) {
        complianceCheckService.changeState(id);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }
}
